home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevtiff.c < prev    next >
C/C++ Source or Header  |  1993-05-20  |  28KB  |  768 lines

  1. /* $Header: /usr/people/sam/fax/gs/RCS/gdevtiff.c,v 1.3 93/03/20 11:44:02 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1992, 1993 Sam Leffler
  5.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /*
  27.  * 5/19/93 modified by L. Peter Deutsch, Aladdin Enterprises,
  28.  *   for compatibility with Ghostscript 2.6.1.
  29.  */
  30. /* gdevtiff.c */
  31.  
  32. #include "gdevprn.h"
  33. #include "gdevdfg3.h"
  34. #include "gdevtiff.h"
  35.  
  36. #ifdef __PROTOTYPES__
  37. #    define PROTO_ARGS(X)    X
  38. #else
  39. #    define PROTO_ARGS(X)    ()
  40. #endif
  41.  
  42. /*
  43.  * TIFF fax output driver.
  44.  */
  45. typedef struct {
  46.     FILE*    fp;
  47.     long    prevdir;    /* file offset of previous directory offset */
  48.     long    diroff;        /* file offset of next write */
  49.     int        bigendian;    /* 1 if machine is big-endian, 0 otherwise */
  50.     int        fax_byte;
  51.     int        fax_weight;
  52. } TIFFOUT;
  53.  
  54. private void    faxout_open_fp PROTO_ARGS((FILE *, TIFFOUT*));
  55. private int    faxout_begin_page PROTO_ARGS((TIFFOUT*, gx_device_printer*));
  56. private int    faxout_eolcode PROTO_ARGS((TIFFOUT *));
  57. private int    faxout_end_page PROTO_ARGS((TIFFOUT *));
  58. private void    tofax PROTO_ARGS((TIFFOUT*, unsigned char*));
  59.  
  60. private void putwhitespan();
  61. private void putblackspan();
  62. private void putcode();
  63. private void puteol();
  64. private void putbit();
  65. private void flushbits();
  66.  
  67. /*
  68.  * Redefine the device descriptor.
  69.  */
  70. struct gx_device_tiff_s {
  71.     gx_device_common;
  72.     gx_prn_device_common;
  73.     TIFFOUT    fax;
  74. };
  75. typedef struct gx_device_tiff_s gx_device_tiff;
  76.  
  77. /* The device descriptor */
  78. #define X_DPI 204
  79. #define Y_DPI 196
  80. #define LINE_SIZE ((X_DPI * 98 / 10 + 7) / 8)    /* bytes per line */
  81.  
  82. private dev_proc_open_device(tiff_prn_open);
  83. private dev_proc_print_page(tiff_print_page);
  84. private dev_proc_close_device(tiff_prn_close);
  85.  
  86. gx_device_procs tiff_std_procs =
  87.     prn_procs(tiff_prn_open, gdev_prn_output_page, tiff_prn_close);
  88.  
  89. gx_device_tiff far_data gs_tiffg3_device =
  90. {   prn_device_std_body(
  91.     gx_device_tiff,
  92.     tiff_std_procs,
  93.     "tiffg3",
  94.     85,            /* width_10ths, 8.5" */
  95.     110,            /* height_10ths, 11" */
  96.     X_DPI, Y_DPI,
  97.     0,0,0,0,        /* margins */
  98.     1,
  99.     tiff_print_page
  100.     )
  101. };
  102.  
  103.  
  104. static struct pageinfo {
  105.     short w, h;            /* page width and height in 10ths */
  106.     unsigned long iw;        /* image width */
  107. } pageinfo[] = {
  108. #define    PAPER_SIZE_LETTER    0
  109.     { 85,  110, 1728 },
  110. #define    PAPER_SIZE_A4        1
  111.     { 83,  117, 1728 },
  112. #define    PAPER_SIZE_B4        2
  113.     { 98, 1391, 2048 }
  114. };
  115. #define    NPAGEINFO (sizeof (pageinfo) / sizeof (pageinfo[0]))
  116.  
  117. /* Get the paper size code, based on width and height. */
  118. static int
  119. papersize(gx_device *dev)
  120. {
  121.     return
  122.       (dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_B4 :
  123.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  124.        PAPER_SIZE_LETTER);
  125. }
  126.  
  127. /*
  128.  * Driver entry points.
  129.  */
  130.  
  131. /*
  132.  * Setup device according to output page.
  133.  */
  134. private int
  135. tiff_prn_open(gx_device *pdev)
  136. {
  137.     struct pageinfo* pi = &pageinfo[papersize(pdev)];
  138.     int    rc;
  139.  
  140.     pdev->width = (int)((pi->w * pdev->x_pixels_per_inch) / 10);
  141.     pdev->height = (int)((pi->h * pdev->y_pixels_per_inch) / 10);
  142.     rc = gdev_prn_open(pdev);
  143.     if (rc == 0) {
  144.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  145.     faxout_open_fp(ddev->file, &ddev->fax);
  146.     }
  147.     return (rc);
  148. }
  149.  
  150. private int
  151. tiff_print_page(gx_device_printer *pdev, FILE *prn_stream)
  152. {
  153.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  154.     unsigned char data[LINE_SIZE + 4];
  155.     int    lnum, line_size;
  156.     TIFFOUT* fax = &ddev->fax;
  157.  
  158.     /* For some odd reason, the file isn't open until now */
  159.     fax->fp = prn_stream;    
  160.     faxout_begin_page(fax, pdev);
  161.     line_size = gdev_mem_bytes_per_scan_line((gx_device*)pdev);
  162.     for (lnum = 0; lnum < pdev->height; lnum++) {
  163.     gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  164.     tofax(fax, data);
  165.     }
  166.     faxout_end_page(fax);
  167.     return (0);
  168. }
  169.  
  170. private int
  171. tiff_prn_close(gx_device *pdev)
  172. {
  173.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  174.     TIFFOUT* fax = &ddev->fax;
  175.  
  176.     if (fax->fp)
  177.     fflush(fax->fp);
  178.     return (gdev_prn_close(pdev));
  179. }
  180.  
  181. /*
  182.  * Internal routines.
  183.  */
  184. private void
  185. faxout_open_fp(FILE *fp, register TIFFOUT* faxp)
  186. {
  187.     faxp->fp = fp;
  188.     faxp->diroff = 0L;
  189.     faxp->prevdir = 0L;
  190.     faxp->bigendian = arch_is_big_endian;
  191.     faxp->fax_byte = 0;
  192.     faxp->fax_weight = 0x80;
  193. }
  194.  
  195. /* NB: this array is sorted by tag number (assumed below) */
  196. typedef struct {
  197.     TIFFDirEntry    subfiletype;
  198.     TIFFDirEntry    imagewidth;
  199.     TIFFDirEntry    imagelength;
  200.     TIFFDirEntry    bitspersample;
  201.     TIFFDirEntry    compression;
  202.     TIFFDirEntry    photometric;
  203.     TIFFDirEntry    fillorder;
  204. #ifdef notdef
  205.     TIFFDirEntry    documentname;
  206. #endif
  207.     TIFFDirEntry    stripoffsets;
  208.     TIFFDirEntry    orientation;
  209.     TIFFDirEntry    samplesperpixel;
  210.     TIFFDirEntry    rowsperstrip;
  211.     TIFFDirEntry    stripbytecounts;
  212.     TIFFDirEntry    xresolution;
  213.     TIFFDirEntry    yresolution;
  214.     TIFFDirEntry    planarconfig;
  215.     TIFFDirEntry    group3options;
  216.     TIFFDirEntry    resolutionunit;
  217. #ifdef notdef
  218.     TIFFDirEntry    software;
  219. #endif
  220.     TIFFDirEntry    cleanfaxdata;
  221.     unsigned long    diroff;            /* offset to next directory */
  222.     unsigned long    xresValue[2];        /* xresolution indirect value */
  223.     unsigned long    yresValue[2];        /* yresolution indirect value */
  224. } TIFFDirectory;
  225. private TIFFDirectory dirTemplate = {
  226.     { TIFFTAG_SUBFILETYPE,    TIFF_LONG,  1, FILETYPE_PAGE },
  227.     { TIFFTAG_IMAGEWIDTH,    TIFF_LONG,  1 },
  228.     { TIFFTAG_IMAGELENGTH,    TIFF_LONG,  1 },
  229.     { TIFFTAG_BITSPERSAMPLE,    TIFF_SHORT, 1, 1 },
  230.     { TIFFTAG_COMPRESSION,    TIFF_SHORT, 1, COMPRESSION_CCITTFAX3 },
  231.     { TIFFTAG_PHOTOMETRIC,    TIFF_SHORT, 1, PHOTOMETRIC_MINISWHITE },
  232.     { TIFFTAG_FILLORDER,    TIFF_SHORT, 1, FILLORDER_MSB2LSB },
  233. #ifdef notdef
  234.     { TIFFTAG_DOCUMENTNAME,    TIFF_ASCII, 1 },
  235. #endif
  236.     { TIFFTAG_STRIPOFFSETS,    TIFF_LONG,  1 },
  237.     { TIFFTAG_ORIENTATION,    TIFF_SHORT, 1, ORIENTATION_TOPLEFT },
  238.     { TIFFTAG_SAMPLESPERPIXEL,    TIFF_SHORT, 1, 1 },
  239.     { TIFFTAG_ROWSPERSTRIP,    TIFF_LONG,  1, -1L },
  240.     { TIFFTAG_STRIPBYTECOUNTS,    TIFF_LONG,  1, 1 },
  241.     { TIFFTAG_XRESOLUTION,    TIFF_RATIONAL, 1 },
  242.     { TIFFTAG_YRESOLUTION,    TIFF_RATIONAL, 1 },
  243.     { TIFFTAG_PLANARCONFIG,    TIFF_SHORT, 1, PLANARCONFIG_CONTIG },
  244.     { TIFFTAG_GROUP3OPTIONS,    TIFF_LONG,  1 },
  245.     { TIFFTAG_RESOLUTIONUNIT,    TIFF_SHORT, 1, RESUNIT_INCH },
  246. #ifdef notdef
  247.     { TIFFTAG_SOFTWARE,        TIFF_ASCII, 1 },
  248. #endif
  249.     { TIFFTAG_CLEANFAXDATA,    TIFF_SHORT, 1, CLEANFAXDATA_CLEAN },
  250.     0, { 0, 1 }, { 0, 1 },
  251. };
  252. #define    OFFSET(x)    ((unsigned)&(((TIFFDirectory*)0)->x))
  253. #define    NTAGS        (OFFSET(diroff) / sizeof (TIFFDirEntry))
  254.  
  255. /* correct tag values on bigendian machines */
  256. private void
  257. faxout_fixuptags(TIFFDirEntry* dp, int n)
  258. {
  259.     while (n-- > 0) {
  260.     if (dp->tdir_type == TIFF_SHORT || dp->tdir_type == TIFF_SSHORT)
  261.         dp->tdir_offset <<= 16;
  262.     else if (dp->tdir_type == TIFF_BYTE || dp->tdir_type == TIFF_SBYTE)
  263.         dp->tdir_offset <<= 24;
  264.     dp++;
  265.     }
  266. }
  267.  
  268. private int
  269. faxout_begin_page(TIFFOUT *faxp, gx_device_printer* pdev)
  270. {
  271.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  272.     short dircount;
  273.     TIFFDirectory dir;
  274.  
  275.     /*
  276.      * Writing the header is delayed to here because the
  277.      * FILE* is not setup when faxout_open is called.
  278.      */
  279.     if (faxp->diroff == 0) {
  280.     TIFFHeader h;
  281.     h.tiff_magic = (faxp->bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  282.     h.tiff_version = TIFF_VERSION;
  283.     h.tiff_diroff = sizeof (TIFFHeader);
  284.     fwrite((char*) &h, sizeof (h), 1, faxp->fp);
  285.     faxp->diroff = sizeof (TIFFHeader);    /* where next directory goes */
  286.     } else {
  287.     /* patch pointer to this directory */
  288.     fseek(faxp->fp, faxp->prevdir, 0);
  289.     fwrite((char*)&faxp->diroff, sizeof (faxp->diroff), 1, faxp->fp);
  290.     }
  291.     fseek(faxp->fp, faxp->diroff, 0);
  292.     /* write count of tags in directory */
  293.     dircount = NTAGS;
  294.     fwrite((char*)&dircount, sizeof (dircount), 1, faxp->fp);
  295.     faxp->diroff += sizeof (dircount);
  296.  
  297.     /* fill in directory tags and write them */
  298.     memcpy(&dir, &dirTemplate, sizeof (dirTemplate));
  299.     dir.imagewidth.tdir_offset = pageinfo[papersize((gx_device*) pdev)].iw;
  300.     dir.imagelength.tdir_offset = pdev->height;
  301.     dir.stripoffsets.tdir_offset = faxp->diroff + sizeof (TIFFDirectory);
  302.     dir.xresolution.tdir_offset = faxp->diroff + OFFSET(xresValue);
  303.     dir.yresolution.tdir_offset = faxp->diroff + OFFSET(yresValue);
  304.     dir.group3options.tdir_offset = 0;        /* XXX */
  305.     dir.xresValue[0] = ddev->x_pixels_per_inch;
  306.     dir.yresValue[0] = ddev->y_pixels_per_inch;
  307.     if (faxp->bigendian)
  308.     faxout_fixuptags(&dir.subfiletype, NTAGS);
  309.     fwrite((char*)&dir, sizeof (dir), 1, faxp->fp);
  310.  
  311.     puteol(faxp);
  312.     return (0);
  313. }
  314.  
  315. private int
  316. faxout_end_page(TIFFOUT *faxp)
  317. {
  318.     long diroff, cc;
  319.  
  320.     flushbits(faxp);
  321.     diroff = faxp->diroff;
  322.     faxp->prevdir = faxp->diroff + OFFSET(diroff);
  323.     faxp->diroff = ftell(faxp->fp);
  324.     /* patch strip byte counts value */
  325.     cc = faxp->diroff - (diroff + sizeof (TIFFDirectory));
  326.     fseek(faxp->fp, diroff + OFFSET(stripbytecounts.tdir_offset), 0);
  327.     fwrite(&cc, sizeof (cc), 1, faxp->fp);
  328.     return (0);
  329. }
  330.  
  331. private const byte far_data b_run_tbl[8][256] = {
  332.   {    /* START BIT 0 */
  333.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  334.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  335.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  336.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  337.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  338.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  339.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  340.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  341.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  342.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  343.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  344.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  345.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  346.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  347.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  348.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  349.   },
  350.   {    /* START BIT 1 */
  351.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  352.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  353.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  354.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  355.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  356.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  357.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  358.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  359.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  360.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  361.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  362.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  363.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  364.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  365.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  366.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  367.   },
  368.   {    /* START BIT 2 */
  369.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  370.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  371.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  372.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  373.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  374.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  375.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  376.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  377.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  378.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  379.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  380.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  381.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  382.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  383.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  384.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  385.   },
  386.   {    /* START BIT 3 */
  387.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  388.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  389.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  390.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  391.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  392.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  393.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  394.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  395.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  396.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  397.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  398.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  399.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  400.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  401.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  402.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  403.   },
  404.   {    /* START BIT 4 */
  405.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  406.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  407.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  408.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  409.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  410.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  411.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  412.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  413.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  414.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  415.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  416.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  417.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  418.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  419.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  420.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  421.   },
  422.   {    /* START BIT 5 */
  423.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  424.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  425.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  426.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  427.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  428.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  429.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  430.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  431.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  432.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  433.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  434.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  435.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  436.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  437.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  438.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  439.   },
  440.   {    /* START BIT 6 */
  441.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  442.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  443.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  444.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  445.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  446.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  447.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  448.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  449.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  450.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  451.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  452.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  453.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  454.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  455.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  456.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  457.   },
  458.   {    /* START BIT 7 */
  459.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  460.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  461.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  462.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  463.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  464.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  465.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  466.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  467.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  468.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  469.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  470.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  471.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  472.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  473.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  474.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  475.   },
  476. };
  477.  
  478. private const byte far_data w_run_tbl[8][256] = {
  479.   {    /* START BIT 0 */
  480.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  481.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  482.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  483.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  484.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  485.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  486.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  487.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  488.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  489.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  490.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  491.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  492.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  493.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  494.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  495.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  496.   },
  497.   {    /* START BIT 1 */
  498.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  499.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  500.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  501.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  502.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  503.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  504.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  505.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  506.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  507.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  508.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  509.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  510.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  511.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  512.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  513.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  514.   },
  515.   {    /* START BIT 2 */
  516.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  517.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  518.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  519.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  520.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  521.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  522.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  523.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  524.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  525.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  526.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  527.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  528.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  529.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  530.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  531.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  532.   },
  533.   {    /* START BIT 3 */
  534.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  535.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  536.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  537.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  538.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  539.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  540.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  541.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  542.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  543.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  544.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  545.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  546.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  547.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  548.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  549.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  550.   },
  551.   {    /* START BIT 4 */
  552.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  553.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  554.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  555.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  556.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  557.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  558.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  559.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  560.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  561.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  562.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  563.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  564.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  565.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  566.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  567.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  568.   },
  569.   {    /* START BIT 5 */
  570.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  571.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  572.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  573.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  574.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  575.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  576.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  577.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  578.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  579.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  580.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  581.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  582.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  583.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  584.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  585.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  586.   },
  587.   {    /* START BIT 6 */
  588.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  589.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  590.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  591.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  592.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  593.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  594.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  595.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  596.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  597.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  598.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  599.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  600.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  601.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  602.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  603.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  604.   },
  605.   {    /* START BIT 7 */
  606.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  607.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  608.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  609.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  610.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  611.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  612.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  613.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  614.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  615.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  616.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  617.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  618.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  619.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  620.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  621.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  622.   },
  623. };
  624.  
  625. /*
  626.  * Macros to use tables in findrun.c
  627.  * Input is *p, bit
  628.  * Output is rl, *p, bit
  629.  */
  630. #define    find_black_run()                    \
  631.     for (rl = 0;;) {                        \
  632.     if (run = b_run_tbl[bit][*p]) {                \
  633.         rl += run;                        \
  634.         bit -= run;                        \
  635.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  636.     }                            \
  637.     break;                            \
  638.     }
  639. #define    find_white_run()                    \
  640.     for (rl = 0;;) {                        \
  641.     if (run = w_run_tbl[bit][*p]) {                \
  642.         rl += run;                        \
  643.         bit -= run;                        \
  644.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  645.     }                            \
  646.     break;                            \
  647.     }
  648.  
  649. private void
  650. tofax(TIFFOUT *faxp, unsigned char *p)
  651. {
  652.     unsigned char    *ep;
  653.     register int    bit;
  654.     register int    run;
  655.     register int    rl;
  656.  
  657.     ep = p + 1728/8;
  658.     bit = 7;
  659.     for (;;) {
  660.     find_white_run();
  661.     putwhitespan(faxp, rl);
  662.     if (p >= ep) break;
  663.  
  664.     find_black_run();
  665.     putblackspan(faxp, rl);
  666.     if (p >= ep) break;
  667.     }
  668.     puteol(faxp);
  669. }
  670.  
  671.  
  672. /*************************************************************************
  673. **
  674. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  675. **
  676. ** Permission to use, copy, modify, and distribute this software and its
  677. ** documentation for any purpose and without fee is hereby granted, provided
  678. ** that the above copyright notice appear in all copies and that both that
  679. ** copyright notice and this permission notice appear in supporting
  680. ** documentation.  This software is provided "as is" without express or
  681. ** implied warranty.
  682.  *************************************************************************/
  683.  
  684. private void
  685. putwhitespan(register TIFFOUT *faxp, int c)
  686. {
  687.     register int tpos;
  688.     register tableentry* te;
  689.  
  690.     if(c>=64) {
  691.     tpos = (c/64)-1;
  692.     te = mwtable+tpos;
  693.     c -= te->count;
  694.     putcode(faxp, te);
  695.     }
  696.     tpos = c;
  697.     te = twtable+tpos;
  698.     putcode(faxp, te);
  699. }
  700.  
  701. private void
  702. putblackspan(register TIFFOUT *faxp, int c)
  703. {
  704.     register int tpos;
  705.     register tableentry* te;
  706.  
  707.     if(c>=64) {
  708.     tpos = (c/64)-1;
  709.     te = mbtable+tpos;
  710.     c -= te->count;
  711.     putcode(faxp, te);
  712.     }
  713.     tpos = c;
  714.     te = tbtable+tpos;
  715.     putcode(faxp, te);
  716. }
  717.  
  718. private void
  719. putcode(register TIFFOUT *faxp, tableentry *te)
  720. {
  721.     register unsigned int mask;
  722.     register int code;
  723.  
  724.     mask = 1<<(te->length-1);
  725.     code = te->code;
  726.     while(mask) {
  727.      if(code&mask)
  728.         putbit(faxp, 1);
  729.     else
  730.         putbit(faxp, 0);
  731.     mask >>= 1;
  732.     }
  733.  
  734. }
  735.  
  736. private void
  737. puteol(register TIFFOUT *faxp)
  738. {
  739.     register int i;
  740.  
  741.     for(i=0; i<11; ++i)
  742.     putbit(faxp, 0);
  743.     putbit(faxp, 1);
  744. }
  745.  
  746. private void
  747. putbit(register TIFFOUT *faxp, int d)
  748. {
  749.     if(d) 
  750.     faxp->fax_byte = faxp->fax_byte|faxp->fax_weight;
  751.     faxp->fax_weight = faxp->fax_weight>>1;
  752.     if((faxp->fax_weight&0xff) == 0) {
  753.     putc(faxp->fax_byte, faxp->fp);
  754.     faxp->fax_byte = 0;
  755.     faxp->fax_weight = 0x80;
  756.     }
  757. }
  758.  
  759. private void
  760. flushbits(register TIFFOUT *faxp)
  761. {
  762.     if (faxp->fax_weight != 0x80) {
  763.     putc(faxp->fax_byte, faxp->fp);
  764.     faxp->fax_byte = 0;
  765.     faxp->fax_weight = 0x80;
  766.     }
  767. }
  768.